<?php
include('rootlogin.php');
//include('../incsession.php');
session_start();

$level_def=array("","EASY", "INTERMEDIATE", "DIFFICULT");

$level=$_SESSION['admin_level'];
//add to database and leave
if($_POST['submit']=="next"){
	
	
	$DND1=$_POST['DND1'];
	for($i=1; $i<=$DND1; $i++){
				$DND2[$i]=$_POST['DND2_'.$i];
				for($j=1; $j<=$DND2[$i]; $j++){
					$DND3[$i][$j]=$_POST['DND3_'.$i.'_'.$j];	
				}
	}
	$FAKE=$_POST['FAKE'];
	$i=1;
	$m=1;
	$excel_row=0;
	while($i <= $DND1)
	{	
		if($_POST['answer'.$i]!='' && $_POST['answer'.$i]!=' '){
			$layer=$m*10000;
			
			
			$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
			$Q = str_replace('"', '&quot;', $Q);
			$A = str_replace("'", "&#39;", $_POST['answer'.$i]);
			$A = str_replace('"', '&quot;', $A);
			$query = "INSERT INTO abstract_drag_and_drop (question, layer, topicid, level) 
				VALUES ('".$Q."',
							'".$layer."', 
							".$_SESSION['topicid'].", ".$level.")";
			$result = mysql_query($query,$orcp_con);
			$query = "INSERT INTO abstract_dnd_answer (answer, layer, topicid, level) 
				VALUES ('".$A."',
							'".$layer."', 
							".$_SESSION['topicid'].", ".$level.")";
			$result = mysql_query($query,$orcp_con);
			
			$n=1;
			$j=1;
			while($j <= $DND2[$i]){
				if($_POST["Q".$i."_".$j]!='' && $_POST["Q".$i."_".$j]!=' '){
					$layer=$m*10000+$n*100;
					$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j]);
					$Q = str_replace('"', '&quot;', $Q);
					$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j]);
					$A = str_replace('"', '&quot;', $A);
					$query = "INSERT INTO abstract_drag_and_drop (question, layer, topicid, level) 
								VALUES ('".$Q."',
								'".$layer."', 
								".$_SESSION['topicid'].", ".$level.")";
					$result = mysql_query($query,$orcp_con);
					$query = "INSERT INTO abstract_dnd_answer (answer, layer, topicid, level) 
								VALUES ('".$A."',
								'".$layer."', 
								".$_SESSION['topicid'].", ".$level.")";
					$result = mysql_query($query,$orcp_con);
					
					
					
					$p=1;
					$k=1;
					while($k <= $DND3[$i][$j]){
						if($_POST["Q".$i."_".$j."_".$k]!='' && $_POST["Q".$i."_".$j."_".$k]!=' '){
							$layer=$m*10000+$n*100+$p;
							$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
							$Q = str_replace('"', '&quot;', $Q);
							$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
							$A = str_replace('"', '&quot;', $A);
							$query = "INSERT INTO abstract_drag_and_drop (question, layer, topicid, level) 
									VALUES ('".$Q."',
									'".$layer."', 
									".$_SESSION['topicid'].", ".$level.")";
							$result = mysql_query($query,$orcp_con);
							$query = "INSERT INTO abstract_dnd_answer (answer, layer, topicid, level) 
									VALUES ('".$A."',
									'".$layer."', 
									".$_SESSION['topicid'].", ".$level.")";
							$result = mysql_query($query,$orcp_con);
							
							$p++;
						}
						$k++;
					}
					$n++;
				}
				$j++;	
			}
			$m++;
		}
		$i++;
	}
	
	
	$i=1;
	while($i<=$FAKE){
		if($_POST['fake'.$i]!='' && $_POST['fake'.$i]!=' '){
				$layer=0;
				$f_ans = str_replace("'", "&#39;", $_POST['fake'.$i]);
				$f_ans = str_replace('"', '&quot;', $f_ans);
				$query = "INSERT INTO abstract_dnd_answer (answer, layer, topicid, level) 
							VALUES ('".$f_ans."',
							'".$layer."', 
							".$_SESSION['topicid'].", ".$level.")";
				$result = mysql_query($query,$orcp_con);
		}
		$i++;
	}
	header("location: article.php");	
}
//value initialization
if(!isset($_POST['DND1'])){
			$DND1=1;
			$DND2[1]=0;
			$DND3[1][1]=0;
			$FAKE=1;
}
else{
	$DND1=$_POST['DND1'];
	for($i=1; $i<=$DND1; $i++){
				$DND2[$i]=$_POST['DND2_'.$i];
				for($j=1; $j<=$DND2[$i]; $j++){
					$DND3[$i][$j]=$_POST['DND3_'.$i.'_'.$j];	
				}
	}
	$FAKE=$_POST['FAKE'];
	if($_POST['submit']=="add a fake answer"){
		$FAKE++;
	}
	else if($_POST['submit']=="add layer 1"){
			$DND1=$_POST['DND1'];
			$DND1++;		
			$DND2[$DND1]=0;
			$DND3[$DND1][1]=0;
	}
	else{
		for($i=1; $i<=$DND1; $i++){
			$str="add layer 2 to Q".$i;
			if($_POST['submit']==$str){
				$DND2[$i]++;
				$DND3[$i][$DND2[$i]]=0;
				break;
			}
			for($j=1; $j<=$DND2[$i]; $j++){
				$str="add layer 3 to Q".$i."-".$j;
				if($_POST['submit']==$str){
					$DND3[$i][$j]++;
					break;
				}
			}
		}
	}
}
?>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="framestyle.css" type="text/css" media="screen">
<style type="text/css">
</style>
<script>
</script>
</head>

<body>
<div id="wrapper">
<div id="title">Online Reading Comprehension Program</div>

<div id="main">
<hr/>
<div id="title2">FOR <?php echo $level_def[$_SESSION['admin_level']]; ?> LEVEL: ABSTRACT - DRAG AND DROP</div>
<hr/><br><br>
<form method = POST action="abstract_drag_and_drop.php" >



<?php
$i=1;

while($i<=$DND1){
	$j=1;
	while($j<=$DND2[$i]){
		echo '<input type="hidden" name="DND3_'.$i.'_'.$j.'" value='.$DND3[$i][$j].'>';
		$j++;
	}
	$Q = str_replace("'", "&#39;", $_POST['Q'.$i]);
	$Q = str_replace('"', '&quot;', $Q);
	$A = str_replace("'", "&#39;", $_POST['answer'.$i]);
	$A = str_replace('"', '&quot;', $A);
	echo '<div class="layer first">
	T'.$i.': <input class="area long" type="hidden" name="Q'.$i.'" value="'.$i.'">
	<input class="area long" type="text" name="answer'.$i.'" value="'.$A.'"><br>
	<input type="hidden" name="DND1" value='.$DND1.'>
	<input type="hidden" name="DND2_'.$i.'" value='.$DND2[$i].'>
	</div>';
	
	$j=1;
	while($j<=$DND2[$i]){
		$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j]);
		$Q = str_replace('"', '&quot;', $Q);
		$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j]);
		$A = str_replace('"', '&quot;', $A);
		echo '<div class="layer second">
			T'.$i.'-'.$j.' : <input class="area medium" type="text" name="Q'.$i.'_'.$j.'" value="'.$Q.'"><br>
			answer: <input class="area medium" type="text" name="answer'.$i.'_'.$j.'" value="'.$A.'"><br>
			<input type="submit" name = "submit" value = "add layer 3 to Q'.$i.'-'.$j.'"></div>';
		$k=1;
		while($k<=$DND3[$i][$j]){
			$Q = str_replace("'", "&#39;", $_POST["Q".$i."_".$j."_".$k]);
			$Q = str_replace('"', '&quot;', $Q);
			$A = str_replace("'", "&#39;", $_POST["answer".$i."_".$j."_".$k]);
			$A = str_replace('"', '&quot;', $A);
			echo '<div class="layer third">
				T'.$i.'-'.$j.'-'.$k.': <input class="area short" type="text" name="Q'.$i.'_'.$j.'_'.$k.'" 
				value="'.$Q.'"><br>
				answer: <input class="area short" type="text" name="answer'.$i.'_'.$j.'_'.$k.'" value="'.$A.'"><br><br></div>';
			$k++;
		}
		$j++;	
	}	
	
	$i++;
	echo "<br><br>";
}
echo '<div class="btn_right"><input type="submit" name = "submit" value = "add layer 1"></div><br><br><br>';
$i=1;
while($i<=$FAKE){
	$f_ans = str_replace("'", "&#39;", $_POST['fake'.$i]);
	$f_ans = str_replace('"', '&quot;', $f_ans);
	echo "<div class='layer fake'>";
	echo 'fake ans: <td><input class="area long" type="text" name="fake'.$i.'" value="'.$f_ans.'">';	
	echo "</div><br>";
	$i++;
}
echo '<div class="btn_right"><input type="submit" name = "submit" value = "add a fake answer"></div>';
echo "<input type='hidden' name='FAKE' value=".$FAKE.">";


?>


<br><br>
<div class="btn_right"><input type="submit" name = "submit" value = "next"></div>
</div>
</form>




</body>

